package com.xujie.sportsmeeting.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujie.sportsmeeting.entity.Registration;
import com.xujie.sportsmeeting.vo.RegistrationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 报名信息 Mapper 接口
 */
@Mapper
public interface RegistrationMapper extends BaseMapper<Registration> {


    /**
     * 分页查询报名信息（包含运动员和项目信息）
     */
    @Select("SELECT r.*, " +
            "a.real_name as athleteName, a.student_number as athleteNumber, " +
            "ce.event_name as eventName, ce.event_code as eventCode " +
            "FROM event_registration r " +
            "LEFT JOIN athlete a ON r.athlete_id = a.id " +
            "LEFT JOIN competition_event ce ON r.event_id = ce.id " +
            "${ew.customSqlSegment}")
    IPage<RegistrationVO> selectRegistrationPage(Page<?> page, @Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<Registration> wrapper);

    /**
     * 根据ID查询报名详情（包含完整的运动员和项目信息）
     */
    @Select("SELECT r.*, " +
            "a.real_name as athleteName, a.student_number as studentNumber, a.gender as athleteGender, " +
            "a.department, a.class_name as className, a.phone, " +
            "ce.event_name as eventName, ce.event_code as eventCode, ce.event_type as eventType, " +
            "ce.gender_limit as genderLimit, ce.age_group as ageGroup, ce.unit, " +
            "ce.competition_time as competitionTime, ce.venue, " +
            "u.real_name as auditUserName " +
            "FROM event_registration r " +
            "LEFT JOIN athlete a ON r.athlete_id = a.id " +
            "LEFT JOIN competition_event ce ON r.event_id = ce.id " +
            "LEFT JOIN sys_user u ON r.audit_user_id = u.id " +
            "WHERE r.id = #{id}")
    RegistrationVO selectRegistrationDetailById(@Param("id") Long id);

    /**
     * 根据运动员ID查询报名信息
     */
    @Select("SELECT r.*, " +
            "ce.event_name, ce.event_code, ce.competition_time, ce.venue " +
            "FROM event_registration r " +
            "LEFT JOIN competition_event ce ON r.event_id = ce.id " +
            "WHERE r.athlete_id = #{athleteId} " +
            "ORDER BY r.create_time DESC")
    List<RegistrationVO> selectByAthleteId(@Param("athleteId") Long athleteId);

    /**
     * 根据项目ID查询报名信息
     */
    @Select("SELECT r.*, " +
            "a.real_name as athlete_name, a.student_number as athlete_number, a.gender as athlete_gender " +
            "FROM event_registration r " +
            "LEFT JOIN athlete a ON r.athlete_id = a.id " +
            "WHERE r.event_id = #{eventId} AND r.status = 1 " +
            "ORDER BY r.create_time ASC")
    List<RegistrationVO> selectByEventId(@Param("eventId") Long eventId);
}
